package com.zzt.controller;

import com.zzt.entity.Gwc;
import com.zzt.entity.Memberinfo;
import com.zzt.service.GwcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@CrossOrigin
@RestController
public class GwcController {
    @Autowired
    GwcService gwcService;


    @GetMapping("/user/showAllGwc")
    public List<Gwc> showAllGwc(HttpServletRequest request){
        HttpSession session = request.getSession();
        Memberinfo user = (Memberinfo)session.getAttribute("user");
        List<Gwc> gwcs = null;
        try {
            gwcs = gwcService.showAllGwc(user.getMno());
        } catch (Exception exception) {
            //购物车空
        }
        return gwcs;
    }

    @GetMapping("/user/addGwcGood/{gwcid}")
    public void addGwcGood(@PathVariable("gwcid") int gwcid){
        gwcService.addOneGoods(gwcid);
    }

    /*  太low的方法  要刷新  浪费流量 不科学
    @GetMapping("/user/addGwcGood")
    public void addGwcGood(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int gwcid = Integer.valueOf(request.getParameter("gwcid"));
        gwcService.addOneGoods(gwcid);
        response.sendRedirect("/gwc.html");
    }*/
    @GetMapping("/user/subGwcGood/{gwcid}")
    public void subGwcGood(@PathVariable("gwcid") int gwcid){
        gwcService.subOneGoods(gwcid);
    }

    @GetMapping("/user/delGood/{gwcid}")
    public void delGood(@PathVariable("gwcid") int gwcid){
        gwcService.delGoods(gwcid);
    }

    @GetMapping("/countGoodsNum")
    public int countGoodsNum(HttpServletRequest request){
        int i=0;
        HttpSession session = request.getSession();
        Memberinfo user = (Memberinfo)session.getAttribute("user");
        if(user!=null){
            i = gwcService.countGoods(user.getMno());
        }
        return i;
    }

    @GetMapping("/user/clearGwc")
    public void clearGwc(HttpServletRequest request){
        HttpSession session = request.getSession();
        Memberinfo user = (Memberinfo)session.getAttribute("user");
        gwcService.clearGwc(user.getMno());
    }
}
